//
//  File.c
//  VideoParse
//
//  Created by xiaerfei on 2020/1/11.
//  Copyright © 2020 erfeixia. All rights reserved.
//

#include "File.h"
#include "URL.h"
#include "avio.h"
#include "common.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/errno.h>

typedef struct FileContext {
    int fd;
    int trunc;
    int blocksize;
    int follow;
#if HAVE_DIRENT_H
    DIR *dir;
#endif
} FileContext;

static int file_priv_open(const char *filename, int flags, ...) {
    int fd;
    unsigned int mode = 0;
    va_list ap;

    va_start(ap, flags);
    if (flags & O_CREAT)
        mode = va_arg(ap, unsigned int);
    va_end(ap);
    
    flags |= O_CLOEXEC;
    
    fd = open(filename, flags, mode);
    
    if (fd != -1) {
        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
            printf("Failed to set close on exec\n");
    }
    
    return fd;
}

static int file_read(URLContext *h, unsigned char *buf, int size) {
    FileContext *c = h->priv_data;
    int ret;
    size = FFMIN(size, c->blocksize);
    ret = (int)read(c->fd, buf, size);
    
    if (ret == 0 && c->follow)
        return EAGAIN;
    if (ret == 0)
        return EOF;
    
    return (ret == -1) ? errno : ret;
}

static int file_write(URLContext *h, const unsigned char *buf, int size) {
    FileContext *c = h->priv_data;
    int ret;
    size = FFMIN(size, c->blocksize);
    ret = (int)write(c->fd, buf, size);
    return (ret == -1) ? errno : ret;
}

static int file_get_handle(URLContext *h) {
    FileContext *c = h->priv_data;
    return c->fd;
}

static int file_open(URLContext *h, const char *filename, int flags) {
    FileContext *c = h->priv_data;
    int access;
    int fd;
    struct stat st;
    
    if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
        access = O_CREAT | O_RDWR;
        if (c->trunc)
            access |= O_TRUNC;
    } else if (flags & AVIO_FLAG_WRITE) {
        access = O_CREAT | O_WRONLY;
        if (c->trunc)
            access |= O_TRUNC;
    } else {
        access = O_RDONLY;
    }
    
    fd = file_priv_open(filename, access, 0666);
    if (fd == -1) { return -1; }
    
    c->fd = fd;
    h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
    if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
        h->min_packet_size = h->max_packet_size = 262144;
    return 0;
}

/* XXX: use llseek */
static int64_t file_seek(URLContext *h, int64_t pos, int whence) {
    FileContext *c = h->priv_data;
    int64_t ret;
    if (whence == AVSEEK_SIZE) {
        struct stat st;
        ret = fstat(c->fd, &st);
        return ret < 0 ? errno : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
    }

    ret = lseek(c->fd, pos, whence);

    return ret < 0 ? errno : ret;
}

static int file_close(URLContext *h) {
    FileContext *c = h->priv_data;
    return close(c->fd);
}

const URLProtocol ff_file_protocol = {
    .name                = "file",
    .url_open            = file_open,
    .url_read            = file_read,
    .url_write           = file_write,
    .url_seek            = file_seek,
    .url_close           = file_close,
    .url_get_file_handle = file_get_handle,
    .url_check           = NULL,
    .url_delete          = NULL,
    .url_move            = NULL,
    .priv_data_size      = sizeof(FileContext),
    .url_open_dir        = NULL,
    .url_read_dir        = NULL,
    .url_close_dir       = NULL,
    .default_whitelist   = "file,crypto"
};
